package com.xiaoerkeji.eshop;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Message;
import android.view.Gravity;

import com.lt.base.BaseActivity;
import com.lt.listener.DataServiceListener;
import com.xiaoerkeji.eshop.bean.RequestInfo;
import com.xiaoerkeji.eshop.listener.MyDialogListener;
import com.xiaoerkeji.eshop.plug.TopBarHelper;
import com.xiaoerkeji.eshop.request.httpRequest;
import com.xiaoerkeji.eshop.ui.dialog.DialogFactory;
import com.xiaoerkeji.eshop.ui.dialog.DialogProgress;
import com.xiaoerkeji.eshop.utils.ComUtils;

import androidx.annotation.Nullable;


public abstract class BaseActivityMy extends BaseActivity implements DataServiceListener
{

    DialogProgress mDialogProgress;
    protected DialogFactory mDialogFactory;
    private httpRequest _request = null;
    TopBarHelper mTopBarHelper;

    /**
     * 访问HTTP接口后返回时的回调 </br>
     *
     * @param msg 返回的消息内容
     */
    protected abstract void receiveMessage(Message msg);

    protected void handleMessage(Message message)
    {
        if (isFinishing())
        {
            return;
        }
        if (message.what > Msg_Base)
        {
            RequestInfo req = ComUtils.formatRequest(message);
            if (req.status == 401)
            {
                return;
            } else if (req.status == -1 && "用户未登录".equals(req.msg))
            {
                if (isShowIng)
                {
                }
                return;
            }
            message.obj = req;
        }
        super.handleMessage(message);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume()
    {
        super.onResume();
    }

    @Override
    protected void onPause()
    {
        super.onPause();
    }

    @Override
    protected void onDestroy()
    {
        closeAllDialog();
        _request = null;
        super.onDestroy();
    }

    @Override
    protected void onStart()
    {
        super.onStart();
    }

    @Override
    protected void onNewIntent(Intent intent)
    {
        super.onNewIntent(intent);
        _request = new httpRequest(this);
    }

    @Override
    protected void initView()
    {
        initSoftKeyboard();
        setRequestedOrientation(getOrientation());
    }

    /**
     * 设置 app 字体不随系统字体设置改变
     */
    @Override
    public Resources getResources()
    {
        Resources res = super.getResources();
        if (res != null)
        {
            Configuration config = res.getConfiguration();
            if (config != null && config.fontScale != 1.0f)
            {
                config.fontScale = 1.0f;
                res.updateConfiguration(config, res.getDisplayMetrics());
            }
        }
        return res;
    }

    protected httpRequest getRequest()
    {
        if (_request == null)
        {
            _request = new httpRequest(this);
        }
        return _request;
    }

    protected TopBarHelper getTopBarHelper()
    {
        if (mTopBarHelper == null)
        {
            mTopBarHelper = new TopBarHelper(this, findViewById(R.id.view_bar));
        }
        return mTopBarHelper;
    }

    public void closeAllDialog()
    {
        if (mDialogProgress != null && mDialogProgress.isShow())
        {
            mDialogProgress.close();
        }
        if (mDialogFactory != null && mDialogFactory.isShow())
        {
            mDialogFactory.close();
        }
        mDialogProgress = null;
        mDialogFactory = null;
    }

    public DialogProgress showProgressDialog(Object message)
    {
        if (!isFinishing())
        {
            closeAllDialog();
            mDialogProgress = new DialogProgress(this);
            mDialogProgress.show(message);
        }
        return mDialogProgress;
    }


    public DialogProgress showProgressDialog()
    {
        return showProgressDialog(null);
    }

    protected void startAct(Class<?> cls)
    {
        startAct(new Intent(this, cls));
    }

    protected void startAct(Intent intent)
    {
        startActivity(intent);
    }

    public void finish()
    {
        super.finish();
    }

    public DialogFactory showDialog(Object title, Object content, Object left, Object right, int gravity, MyDialogListener listener)
    {
        if (!isFinishing())
        {
            closeAllDialog();
            mDialogFactory = new DialogFactory(this);
            mDialogFactory.show(title, content, left, right, gravity, listener);
        }
        return mDialogFactory;
    }

    public DialogFactory showDialog(Object content, Object left, Object right, MyDialogListener listener)
    {
        return showDialog(null, content, left, right, Gravity.CENTER, listener);
    }

    protected DialogFactory showDialog(Object content, Object button, MyDialogListener listener)
    {
        return showDialog(null, content, button, null, Gravity.CENTER, listener);
    }

    public DialogFactory showDialogTips(Object content)
    {
        return showDialog(null, content, null, null, Gravity.CENTER, null);
    }

    public DialogFactory showDialogTips(Object title, Object content)
    {
        return showDialog(title, content, null, null, Gravity.CENTER, null);
    }

    protected DialogFactory showDialogTips(Object content, MyDialogListener listener)
    {
        return showDialog(null, content, null, null, Gravity.CENTER, listener);
    }

    public void addNotification(String name)
    {
        xeApp.getInstance().getNotificationCenter().removeObserver(this, name);
        xeApp.getInstance().getNotificationCenter().addObserver(this, name, name);
    }

    /**
     * 初始化软键盘
     */
    protected void initSoftKeyboard()
    {
        // 点击外部隐藏软键盘，提升用户体验
        getContentView().setOnClickListener(v -> hideSoftKeyboard());
    }

    /**
     * @return 设置界面横竖屏
     * ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE 横屏模式
     * ActivityInfo.SCREEN_ORIENTATION_PORTRAIT 竖屏模式
     * ActivityInfo.SCREEN_ORIENTATION_SENSOR 跟随系统sensor的状态
     */
    protected int getOrientation()
    {
        return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    }

//    public void startLogin()
//    {
//        log("start login 1");
//        xeSetting.getInstance().clearLogin();
//        _request = null;
//        Intent intent = new Intent();
//        intent.setClass(this, ActSplash.class);
//        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        startAct(intent);
//    }
}
